package cn.b.sky.mes.andon.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.b.sky.mes.andon.dao.AndonGroupAssignDao;
import cn.b.sky.mes.andon.dao.AndonGroupDao;
import cn.b.sky.mes.andon.model.AndonGroup;
import cn.b.sky.mes.andon.model.AndonGroupAssign;
import cn.b.sky.mes.andon.service.AndonGroupService;

/**
 * 
 * <strong>Title : AndonGroupServiceImpl.java<br></strong>
 * <strong>Package : cn.b.sky.mes.andon.service.impl<br></strong>
 * <strong>Description : </strong>按灯组服务实现类<br> 
 * <strong>Create on : 2016年10月17日 上午9:29:42<br></strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人|修改日期|修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Service("andonGroupService")
public class AndonGroupServiceImpl implements AndonGroupService {

  @Autowired
  private AndonGroupDao andonGroupDao;

  @Autowired
  private AndonGroupAssignDao andonGroupAssignDao;

  @Override
  public List<AndonGroup> getAllAndonGroups(String siteCode) {
    return andonGroupDao.getAllAndonGroups(siteCode);
  }

  @Override
  public List<AndonGroup> getAndonGroupByWorkUnit(String siteCode, String workUnitCode) {
    return andonGroupDao.getAndonGroupByWorkUnit(siteCode, workUnitCode);
  }

  @Override
  public AndonGroup getAndonGroupByName(String siteCode, String workUnitCode, String andonGroup) {
    return andonGroupDao.getAndonGroupByName(siteCode, workUnitCode, andonGroup);
  }

  @Override
  public void insertAndonGroup(AndonGroup group) {
    andonGroupDao.insertAndonGroup(group);
  }

  @Override
  public void updateAndonGroup(AndonGroup group) {
    // 原组名和新组名都存在且不一样时更新
    if (!StringUtils.isEmpty(group.getOriginalAndonGroup()) && !StringUtils.isEmpty(group.getAndonGroup())
        && !group.getAndonGroup().equals(group.getOriginalAndonGroup())) {
      // 查询原始组关系
      AndonGroup originalAndonGroup = new AndonGroup();
      originalAndonGroup.setSiteCode(group.getSiteCode());
      originalAndonGroup.setWorkUnitCode(group.getWorkUnitCode());
      originalAndonGroup.setAndonGroup(group.getOriginalAndonGroup());
      List<AndonGroupAssign> assigns = andonGroupAssignDao.getAndonGroupAssignsByGroup(originalAndonGroup);
      // 删除原始组关系
      andonGroupAssignDao.batchDeleteAndonGroupAssigns(originalAndonGroup);
      // 更新组名
      andonGroupDao.updateAndonGroup(group);
      // 保存新的组关系
      for (AndonGroupAssign assign : assigns) {
        assign.setAndonGroup(group.getAndonGroup());
        andonGroupAssignDao.insertAndonGroupAssign(assign);
      }
    }
  }

  @Override
  public void deleteAndonGroup(AndonGroup group) {
    // 删除组关系
    andonGroupAssignDao.batchDeleteAndonGroupAssigns(group);
    // 删除组
    andonGroupDao.deleteAndonGroup(group);
  }

}
